package com.syh.jn.scm.infrastructure.repository.impl.upstream.finance;

import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AggregateRepoMybatisImplTmpl;
import com.syh.jn.scm.domain.upstream.repository.finance.UpStatementAccountBillCartReadRepository;
import com.syh.jn.scm.dto.upstream.finance.upStatementAccountBillCart.UpStatementAccountBillCartVo;
import com.syh.jn.scm.infrastructure.repository.model.finance.StatementAccountBillCartModel;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("upStatementAccountBillCartReadRepositoryMbImpl")
public class UpStatementAccountBillCartReadRepositoryMbImpl implements UpStatementAccountBillCartReadRepository {

  private AggregateRepoMybatisImplTmpl<UpStatementAccountBillCartVo, StatementAccountBillCartModel, Long> statementAccountBillCartMbImpl =
          new AggregateRepoMybatisImplTmpl.Builder().modelClass(StatementAccountBillCartModel.class).listTypeClass(UpStatementAccountBillCartVo.class).build();

  @Override
  public List<UpStatementAccountBillCartVo> getList(Wrapper wrapper) {
    return this.statementAccountBillCartMbImpl.getList(wrapper);
  }

  public UpStatementAccountBillCartReadRepositoryMbImpl() {
    SQL statementAccountBillCartSql = new SQL(){{
      SELECT(SQLAliasGenerateUtil.aliasGenerate(StatementAccountBillCartModel.class,"upStatementAccountBillCart",null),
              "(" +
              " IFNULL($saleOrder.price, 0) " +
              "+ IFNULL($saleReturnOrder.price, 0) " +
              "+ IFNULL($purOrder.price, 0) " +
              "+ IFNULL($purReturnOrder.price, 0) " +
              "+ IFNULL($costBill.price, 0) " +
              "+ IFNULL($incomeBill.price, 0) " +
              " ) price, ( " +
              " CASE upStatementAccountBillCart.SOURCE_TYPE " +
              " WHEN 0 THEN " +
              " IF(ISNULL($saleOrder.price),0,1) " +
              " WHEN 1 THEN " +
              " IF(ISNULL($saleReturnOrder.price),0,1)" +
              " WHEN 2 THEN " +
              " IF(ISNULL($purOrder.price),0,1) " +
              " WHEN 3 THEN " +
              " IF(ISNULL($purReturnOrder.price),0,1) " +
              " WHEN 4 THEN " +
              " IF(ISNULL($costBill.price),0,1) " +
              " WHEN 5 THEN " +
              " IF(ISNULL($incomeBill.price),0,1) " +
              " ELSE " +
              " - 1 " +
              " END " +
              " ) AS statementState");
      FROM("T_FINANCE_STATEMENT_ACCOUNT_BILL_CART upStatementAccountBillCart");
      LEFT_OUTER_JOIN("(  " +
              "  SELECT  " +
              "  ID id,  " +
              "  `CODE` `code`,  " +
              "  DOWN_STREAM_COMPANY_ID customerId,  " +
              "  TOTAL_OUT_AMOUNT price  " +
              "  FROM  " +
              "  `t_sale_order` saleOrder  " +
              "  WHERE  " +
              "  saleOrder.COLLECT_STATE <> 0  " +
              "  AND saleOrder.ORD_STATE = 1  " +
              ") $saleOrder ON $saleOrder.id = upStatementAccountBillCart.SOURCE_ID  " +
              "AND upStatementAccountBillCart.SOURCE_TYPE = 0");
      LEFT_OUTER_JOIN("(  " +
              "  SELECT  " +
              "  ID id,  " +
              "  `CODE` `code`,  " +
              "  DOWN_STREAM_COMPANY_ID customerId,  " +
              "  RETURN_TOTAL_IN_AMOUNT price  " +
              "  FROM  " +
              "  `t_sale_return_order` saleReturnOrder  " +
              "  WHERE  " +
              "  saleReturnOrder.COLLECT_STATE <> 0  " +
              "  AND saleReturnOrder.RETURN_ORD_STATE = 1  " +
              ") $saleReturnOrder ON $saleReturnOrder.id = upStatementAccountBillCart.SOURCE_ID  " +
              "AND upStatementAccountBillCart.SOURCE_TYPE = 1");
      LEFT_OUTER_JOIN("(" +
              " SELECT " +
              " ID id," +
              " `CODE` `code`," +
              " UP_STREAM_COMPANY_ID customerId," +
              " TOTAL_IN_AMOUNT price" +
              " FROM" +
              " `t_pur_purchase_order` purOrder" +
              " WHERE " +
              " purOrder.COLLECT_STATE <> 0" +
              " AND purOrder.ORD_STATE = 1" +
              ") $purOrder ON $purOrder.id = upStatementAccountBillCart.SOURCE_ID " +
              " AND  upStatementAccountBillCart.SOURCE_TYPE = 2 ");
      LEFT_OUTER_JOIN("(" +
              " SELECT " +
              " ID id," +
              " `RETURN_CODE` `code`, " +
              " UP_STREAM_COMPANY_ID customerId, " +
              " RETURN_TOTAL_OUT_AMOUNT price " +
              " FROM " +
              " `t_pur_purchase_return_order` purReturnOrder " +
              " WHERE " +
              " purReturnOrder.COLLECT_STATE <> 0 " +
              " AND purReturnOrder.RETURN_ORD_STATE = 1 " +
              ") $purReturnOrder ON $purOrder.id =  upStatementAccountBillCart.SOURCE_ID " +
              "AND  upStatementAccountBillCart.SOURCE_TYPE = 3 ");
      LEFT_OUTER_JOIN("( " +
              " SELECT " +
              " ID id, " +
              " `CODE` `code`, " +
              " CUSTOMER_ID customerId, " +
              " PRICE price " +
              " FROM " +
              " `t_finance_cost_bill` costBill " +
              " WHERE " +
              " costBill.STATE = 1 " +
              " AND costBill.CONFIRM_STATE = 1 " +
              ") $costBill ON $costBill.id =  upStatementAccountBillCart.SOURCE_ID " +
              "AND  upStatementAccountBillCart.SOURCE_TYPE = 4");
      LEFT_OUTER_JOIN("( " +
              " SELECT " +
              " ID id, " +
              " `CODE` `code`, " +
              " CUSTOMER_ID customerId, " +
              " PRICE price " +
              " FROM " +
              " `t_finance_income_bill` incomeBill " +
              " WHERE " +
              " incomeBill.STATE = 1 " +
              " AND incomeBill.CONFIRM_STATE = 1 " +
              ") $incomeBill ON $incomeBill.id =  upStatementAccountBillCart.SOURCE_ID " +
              "AND  upStatementAccountBillCart.SOURCE_TYPE = 5");
    }};
    this.statementAccountBillCartMbImpl.setQuerySql(statementAccountBillCartSql);
    Map<String,String> map = new HashMap<>();
    map.put("businessEntityId","upStatementAccountBillCart.BUSINESS_ENTITY_ID");
    map.put("customerId","upStatementAccountBillCart.CUSTOMER_ID");
    map.put("customerType","upStatementAccountBillCart.CUSTOMER_TYPE");
    map.put("brokerageId","upStatementAccountBillCart.BROKERAGE_ID");
    map.put("sourceType","upStatementAccountBillCart.SOURCE_TYPE");
    map.put("sourceId","upStatementAccountBillCart.SOURCE_ID");
    this.statementAccountBillCartMbImpl.setWrapperParseMethods((propertyName) -> {
      return map.get(propertyName);
    });


  }

}
